package com.rfid.util;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.rfid.a252robot.App;
import com.rfid.db.bean.Book;
import com.rfid.manager.DBManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Util {


    public static String getDate(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }

    public static Date getDate(String format, String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }

    public static String getDateAgo(String format, int monthAgo) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, monthAgo);
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String getDate(String format, int dayAgo) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, dayAgo);
        Date date = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String getDate(String format, String dateStr, int dayAgo) throws ParseException {
        Date date = getDate(format,dateStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dayAgo);
        Date date1 = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date1);
    }

    public static Dialog createDialog(Context context, String text) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCanceledOnTouchOutside(false);
        if (!TextUtils.isEmpty(text)) {
            dialog.setMessage(text);
        }
        return dialog;
    }

    public static void startActivity(Context context,Class clazz) {
        Intent intent = new Intent();
        intent.setClass(context,clazz);
        context.startActivity(intent);
    }

    public static int getAntenna(int boxNum) {
        if (boxNum <= 9) {
            return 1;
        } else if (boxNum <= 28) {
            return 2;
        } else if (boxNum <= 37) {
            return 3;
        } else {
            return 4;
        }
    }

    public static List<Integer> getBoxes(int antenna) {
        List<Integer> boxes = new ArrayList<>();
//        int start = 1;
//        int end = -1;
//        if (antenna == 1) {
//            start = 1;
//            end = 9;
//        } else if (antenna == 2) {
//            start = 15;
//            end = 23;
//        } else if (antenna == 3) {
//            start = 29;
//            end = 37;
//        } else if (antenna == 4) {
//            start = 43;
//            end = 51;
//        }
//        for (int i=start;i<=end;i++) {
//            boxes.add(Integer.valueOf(i));
//        }
        for (int i=1;i<=9;i++) {
            boxes.add(Integer.valueOf(i + (antenna-1)*14));
        }
        return boxes;
    }

    public static List<Integer> getBoxNums() {
        List<Integer> boxes = new ArrayList<>();
        for (int row=0;row<4;row++) {
            for (int i=1;i<=9;i++) {
                boxes.add(Integer.valueOf(i + row*14));
            }
        }
        return boxes;
    }

    public static int[] getAllLockNums() {
        //6块板子
        int par[] = new int[8];
        for (int i=0;i<par.length;i++) {
            par[i] = i*7 + 1;
        }
        return par;
    }

    public static String getBoxShow(int boxNum) {
        String show = "";
        if(boxNum > 28) {
            boxNum = boxNum - 28;
            if (boxNum <= 9) {
                show = "B" + boxNum;
            } else {
                show = "B" + (boxNum - 14 + 9);
            }
        } else {
            if (boxNum <=9) {
                show = "A" + boxNum;
            } else {
                show = "A" + (boxNum - 14 + 9);
            }
        }
        return show;
    }

    public static int getBoxRow(int boxNum) {
        if (boxNum <= 14) {
            return 0;
        } else if (boxNum <= 28) {
            return 1;
        } else if (boxNum <= 42) {
            return 2;
        } else {
            return 3;
        }
    }

    public static List<Integer> getEmptyBoxes() {
        List<Integer> boxes = Util.getBoxNums();
        List<Book> books = DBManager.getBooks();
        for (Book book: books) {
            if (boxes.contains(Integer.valueOf(book.getBoxNum()))) {
                boxes.remove(Integer.valueOf(book.getBoxNum()));
            }
        }
        return boxes;
    }

   public static int getStatusBarHeight(Context context)
    {
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }

    public static int getScreenHeight() {
        WindowManager wm = (WindowManager) App.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return -1;
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;
    }

    public static String getCover(String isbn) {
        return String.format("http://www.bookcovers.cn/index.php?client=clcn&isbn=%s/cover",isbn);
    }

}
